<%@ page import="java.util.Arrays" %>
<%@ page import="com.bobo.model.Message" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!doctype html>
<html class="no-js" lang="">
<head>
    <jsp:include page="../layout/head.jsp">
        <jsp:param name="title" value="文章阅览"/>
        <jsp:param name="cssName" value="topic"/>
    </jsp:include>
    <script src="${pageContext.request.contextPath}/public/front/scripts/jquery.min.js"></script>
</head>
<body>
<!--[if lt IE 10]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade
    your browser</a> to improve your experience.</p>
<![endif]-->

<%--<script>--%>
    <%--&lt;%&ndash;onload="query(${topic.id})"&ndash;%&gt;--%>
    <%--&lt;%&ndash;function query(id) {&ndash;%&gt;--%>
        <%--&lt;%&ndash;$.ajax({&ndash;%&gt;--%>
            <%--&lt;%&ndash;url: "${pageContext.request.contextPath}/topic/query",&ndash;%&gt;--%>
            <%--&lt;%&ndash;type: "get",&ndash;%&gt;--%>
            <%--&lt;%&ndash;data: {"id": id},&ndash;%&gt;--%>
            <%--&lt;%&ndash;success: function (data) {&ndash;%&gt;--%>
                <%--&lt;%&ndash;if (data == "true") {&ndash;%&gt;--%>
                    <%--&lt;%&ndash;document.getElementById("sc").value = "已收藏";&ndash;%&gt;--%>
                <%--&lt;%&ndash;} else {&ndash;%&gt;--%>
                    <%--&lt;%&ndash;if (data == "false") {&ndash;%&gt;--%>
                        <%--&lt;%&ndash;document.getElementById("sc").value = "收藏";&ndash;%&gt;--%>
                    <%--&lt;%&ndash;}&ndash;%&gt;--%>
                <%--&lt;%&ndash;}&ndash;%&gt;--%>
            <%--&lt;%&ndash;}&ndash;%&gt;--%>
        <%--&lt;%&ndash;})&ndash;%&gt;--%>
    <%--&lt;%&ndash;}&ndash;%&gt;--%>

<%--</script>--%>
<jsp:include page="../layout/nav.jsp"/>

<div class="container">


    <article class="main">

        <div class="bread-nav">
            <ul class="clearfix">
                <li><a href="${pageContext.request.contextPath}/index/blog">首页</a></li>
                <li><a href="${pageContext.request.contextPath}/index/blog?categoryId=${topic.category.id}">${topic.category.name}</a></li>
                <li><a href="#">${topic.title}</a></li>
            </ul>
        </div>

        <div class="content-wrapper">
            <h1 class="title">${topic.title}</h1>
            <p class="crate-time">
                <time>发表时间${topic.createTime} &nbsp;&nbsp;更新时间${topic.updateTime}</time>
                <span>
                     <c:if test="${user==null}"><input type="button" value="收藏" disabled>请先登录</c:if>
                     <c:if test="${user!=null}">

                         <c:if test="${isFav}">
                             <input type="button" id="sc" class="${topic.id}" value="已收藏" onclick="collection(this)">
                         </c:if>
                         <c:if test="${!isFav}">
                             <input type="button" id="sc" class="${topic.id}" value="收藏" onclick="collection(this)">
                         </c:if>

                     </c:if>
                </span>
            </p>

            <div class="tags">
                <c:forEach items="${topic.tags}" var="tag">
                    <a href="${pageContext.request.contextPath}/index/blog?tagId=${tag.id}">${tag.name}</a>
                </c:forEach>
            </div>
            <div class="content-main">${topic.content}</div>
            <div>
                评论区：


                <hr>
                <c:if test="${page.list==null}">还没有被评论</c:if>
                <c:if test="${page.list!=null}">
                    <% List<Integer> reps =  (List<Integer>)request.getAttribute("reps");%>

                    <c:forEach items="${page.list}" var="message">
                        <span>评论:${message.content}</span><br>
                        评论人${message.user.name}&nbsp;&nbsp;${message.createTime}

                        <c:if test="${user!=null}">


                            <% Message msg = (Message)pageContext.getAttribute("message"); %>


                            <% if(!reps.contains(msg.getId())) {%>
                                <button id="${message.id}" onclick="report(${message.id})">举报</button>
                            <% }%>


                        </c:if>
                        <hr>
                    </c:forEach>
                </c:if>
                <form action="${pageContext.request.contextPath}/message/add" method="post">
                    <input type="hidden" name="id" value="${topic.id}">
                    评论：
                    <textarea name="content"></textarea><br>
                    <c:if test="${user==null}"><input type="submit" value="提交" disabled>登录后方可留言</c:if>
                    <c:if test="${user!=null}"><input type="submit" value="提交"></c:if>
                </form>
            </div>
        </div>



    </article>

    <jsp:include page="../layout/right.jsp"/>
</div>
<jsp:include page="../layout/footer.jsp"/>
<script>
    function collection(id) {
        var d = id.value;
        if (d == "收藏") {
            $.ajax({
                url: "${pageContext.request.contextPath}/user/addFavourite",
                type: "get",
                data: {"id": id.className},
                success: function (data) {
                    if (data == "true") {
                        document.getElementById("sc").value = "已收藏";
                    }
                }
            })
        } else {
            $.ajax({
                url: "${pageContext.request.contextPath}/user/delFavourite",
                type: "get",
                data: {"id": id.className},
                success: function (data) {
                    if (data == "true") {
                        document.getElementById("sc").value = "收藏";
                    }
                }
            })
        }

    }


    function report(id) {
        var content = prompt("请输入理由");
        if(content) {
            $.ajax({
                url: "${pageContext.request.contextPath}/report/add",
                type: "get",
                data: {"id": id,"content":content},
                success: function (data) {
                    if (data == "true") {
                        document.getElementById("id").innerHTML = "已举报";
                        document.getElementById("id").disabled=true;
                    }
                }
            })
        }

    }
</script>
</body>
</html>
